package com.Action;


import com.Services.UserService;
import com.opensymphony.xwork2.ActionSupport;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2017/2/23.
 */
public class RegisterAction extends ActionSupport {
    private String username;
    private String userNumber;
    private String password;
    private String confirmPassword;
    private String superpassword;
    private String confirmSuperPassword;
    private String sex;
    private String yzm;
    private String year;
    private String month;
    private String cellphone;
    private String address;
    private String education;
    private String idCard;

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public String getCellphone() {
        return cellphone;
    }

    public void setCellphone(String cellphone) {
        this.cellphone = cellphone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEducation() {
        return education;
    }

    public void setEducation(String education) {
        this.education = education;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public String getSuperpassword() {
        return superpassword;
    }

    public void setSuperpassword(String superpassword) {
        this.superpassword = superpassword;
    }

    public String getConfirmSuperPassword() {
        return confirmSuperPassword;
    }

    public void setConfirmSuperPassword(String confirmSuperPassword) {
        this.confirmSuperPassword = confirmSuperPassword;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUserNumber() {
        return userNumber;
    }

    public void setUserNumber(String userNumber) {
        this.userNumber = userNumber;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getYzm() {
        return yzm;
    }

    public void setYzm(String yzm) {
        this.yzm = yzm;
    }

    @Override
    public String execute() throws Exception {
        HttpServletRequest request= ServletActionContext.getRequest();
        request.setCharacterEncoding("utf-8");
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService us=ac.getBean(UserService.class);
        if( yzm.equals(request.getSession().getAttribute("random"))){
            //给对应服务传参
            String flag=us.register(username,userNumber, password,confirmPassword,superpassword,confirmSuperPassword,
                    sex,year,month,cellphone,address,education,idCard);
            if(flag.equals("success")){
                //注册成功自动登录,把用户信息存入会话
                if(request.getAttribute("registermessage")!=null)
                    request.removeAttribute("registermessage");
                request.getSession().setAttribute("Account",userNumber);
                request.getSession().setAttribute("privilege",1);
                return SUCCESS;
            }else if(flag.equals("user")){
                request.setAttribute("registermessage","提示:用户名已存在！");
            }else if(flag.equals("password")){
                request.setAttribute("registermessage","提示:输入的两次密码不一致！");
            }else if(flag.equals("super")){
                request.setAttribute("registermessage","提示:输入的两次超级密码不一致！");
            }
            return ERROR;
        }else{
            request.setAttribute("registermessage","提示:验证码错误！");
            return "yzmerror";
        }
    }
}
